/*
 * The MIT License
 *
 * Copyright 2015 chronosinfo.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * Autor John Vanderson M Lima
 */
package com.chronos.pafecf.db;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.AuthProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author usuario
 */
public class AcessoDB {

    Connection con;
    Connection conRetaguarda;
    String bancoPaf;
    String bancoRetaguarda;
    String usrDB;
    String senhaDB;
    String portaDB;
    String usrDBRetaguarda;
    String senhaDBRetaguarda;
    String portaDBRetaguarda;
    String hostDB;
    String hostDBRetaguarda;
    String nomeDB;
    String nomeDBRetaguarda;
    String urlDB;
    String urlDBRegaguarda;

    public Connection conectarDB() {
        carregarArquivoConexao();
        try {
            switch (bancoPaf) {
                case "Mysql":
                    urlDB = "jdbc:mysql://" + hostDB + ":" + portaDB + "/" + nomeDB;
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection(urlDB, usrDB, senhaDB);
                case "FireBird":
                    urlDB = "jdbc:firebirdsql:" + hostDB + "/3050:database/" + nomeDB;
                    Class.forName("org.firebirdsql.jdbc.FBDriver");
                    con = DriverManager.getConnection(urlDB, "SYSDBA", "MASTERKEY");
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar o Driver!", "Aviso do Sistema", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Não foi possível conectar ao banco!", "Aviso do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
        return con;
    }

    public Connection conectarDBRetaguarda() {
        carregarArquivoConexao();
        try {
            switch (bancoPaf) {
                case "Mysql":
                    urlDB = "jdbc:mysql://" + hostDB + ":" + portaDB + "/" + nomeDB;
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection(urlDB, usrDB, senhaDB);
                case "FireBird":
                    urlDB = "jdbc:firebirdsql:" + hostDB + "/3050:"+ nomeDB;
                    Class.forName("org.firebirdsql.jdbc.FBDriver");
                    con = DriverManager.getConnection(urlDB, "SYSDBA", "MASTERKEY");
                case "PostgreSQL":
                    urlDB = "jdbc:postgresql://" + hostDB + portaDBRetaguarda + "/" + nomeDBRetaguarda;
                    Class.forName("org.postgresql.Driver");
                    conRetaguarda = DriverManager.getConnection(urlDB, usrDBRetaguarda, senhaDBRetaguarda);
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar o Driver!", "Aviso do Sistema", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Não foi possível conectar ao banco!", "Aviso do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
        return conRetaguarda;
    }

    public void carregarArquivoConexao() {
        /**
         * carrega as configurações da conexão
         */

        Properties arquivoConexao = new Properties();
        try {
            arquivoConexao.load(new FileInputStream(System.getProperty("user.dir")
                    + System.getProperty("file.separotor")
                    + "conexao.properties"));

            bancoPaf = arquivoConexao.getProperty("sgbd.DB");
            usrDB = arquivoConexao.getProperty("sgbd.usrDB");
            senhaDB = arquivoConexao.getProperty("sgbd.senhaDB");
            portaDB = arquivoConexao.getProperty("sgbd.portaDB");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Arquivo de conexao não localizado");

        }

    }

     public void desconectar() {
        try {
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void desconectarRetaguarda() {
        try {
            conRetaguarda.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

}
